package com.example.ztpandroid233;

import static java.lang.System.out;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.ftpserver.FtpServerConfigurationException;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.usermanager.ClearTextPasswordEncryptor;



import com.ztp.android.usermanager.AndroidDbUserManagerFactory;
import com.ztp.lib.server.Env;
import com.ztp.lib.server.ServerHelper;





import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
	
	ServerHelper sh = null;
	
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//SQLiteHelper helper = new SQLiteHelper(this);
		main();
		
		
	}
	
	
	void main(){
		String path = getCacheDir().getAbsolutePath();
		Log.i("path", path);
		Env.setDataDir(path);
		Log.i("path", Env.getDataDir());
		
		
		
		SQLiteHelper helper = new SQLiteHelper(this);
		/*SQLiteDatabase db = helper.getWritableDatabase();
		db.close();*/
		
		if(sh == null){
			try {
				sh = new ServerHelper();
				sh.umf = new AndroidDbUserManagerFactory("admin", new ClearTextPasswordEncryptor(), helper);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		final ToggleButton start = (ToggleButton) findViewById(R.id.toggleButton1);
		start.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				Log.i("ToggleButton start", "" + start.isChecked());
				
				if(isChecked){
					if(sh.server.isStopped()){
						try {
							sh.startServer();
						} catch (FtpException e) {
							e.printStackTrace();
							buttonView.setChecked(false);
						}
						catch(FtpServerConfigurationException e){
							e.printStackTrace();
							Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
							buttonView.setChecked(false);
						}
					}
					else{
						buttonView.setChecked(false);
					}
				}
				else{
					if(!sh.server.isStopped()){
						sh.stopAll();
						
					}
					else{
						buttonView.setChecked(true);
					}
				}
			}
		});
	}
	
	@Override
	protected void onPause() {
		
		super.onPause();
		
	}
	
	@Override
	protected void onStop() {
		Log.i("onStop", "onStop");
		sh.stopAll();
		sh = null;
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
